package zjc.bookkeeping.minepage.zAcountBinding;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import zjc.bookkeeping.R;
import zjc.bookkeeping.db.DBManager;
import zjc.bookkeeping.db.UserBean;
import zjc.bookkeeping.minepage.dAccountbookmanagement.AccountbookmanagementMainActivity;
import zjc.bookkeeping.tools.TextDialog;

public class AcountBindingActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView nickname;
    private TextView phone_number;
    private TextView qq;
    private TextView zfb;
    TextView wx;
    Button exit;
    ImageButton back;
    UserBean userBean = new UserBean();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_acount_binding);
        userBean = DBManager.findUser();
        initView();
        if (userBean != null){
            setUserText();
        }
//        SharedPreferences preferences = getSharedPreferences("userInfo",
//                Activity.MODE_PRIVATE);
////        if ()
//  //      String account = preferences.getString("account", "");
    }

    private void setUserText() {
        if (userBean.getUser_name()!= null &&!userBean.getUser_name().equals("null")){
            Log.e("用户信息",userBean.getUser_name());
            nickname.setText(userBean.getUser_name());
        }
        if (userBean.getUser_phone()!= null &&!userBean.getUser_phone().equals("null")){
            Log.e("用户信息",userBean.getUser_phone());
            phone_number.setText(userBean.getUser_phone());
        }
        if (userBean.getUser_qq()!= null &&!userBean.getUser_qq().equals("null")){
            Log.e("用户信息",userBean.getUser_qq());
            qq.setText("已绑定");
        }
        if (userBean.getUser_zfb()!= null &&!userBean.getUser_zfb().equals("null")){
            Log.e("用户信息",userBean.getUser_zfb());
            zfb.setText("已绑定");
        }
        if (userBean.getUser_wx()!= null &&!userBean.getUser_wx().equals("null")){
            Log.e("用户信息",userBean.getUser_wx());
            wx.setText("已绑定");
        }
    }

    private void initView(){
        nickname = this.findViewById(R.id.nickname);
        phone_number = this.findViewById(R.id.phone_number);
        qq = this.findViewById(R.id.QQ);
        zfb = this.findViewById(R.id.zfb);
        wx = findViewById(R.id.wx);

        back = this.findViewById(R.id.user_back);
        back.setOnClickListener(this);
        exit = this.findViewById(R.id.user_exit_btn);
        exit.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.user_back:
                finish();
                break;
            case R.id.user_exit_btn:
                if (userBean != null){
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setMessage("是否退出登录？本地数据将进行清空");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            DBManager.deleteUser();
                            DBManager.resetTable();
                            DBManager.insertSelectAccount(0,1);
                            initText();
                        }
                    });
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                        }
                    });
                    builder.create().show();
                }
                break;
        }
    }

    private void initText() {
        nickname.setText("未登录");
        phone_number.setText("未绑定");
        qq.setText("未绑定");
        zfb.setText("未绑定");
        wx.setText("未绑定");
    }
}